package com.base.lib.ui;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.base.lib.R;
import com.base.lib.mvp.present.BasePresenter;
import com.base.lib.mvp.view.IBaseView;
import com.base.lib.net.listener.HttpResponseListener;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.Collections;
import java.util.List;

/**
 * @author neo.duan
 * @date 2019-09-29 14:51
 * @desc Fragment分页基类
 */
public abstract class BasePageFragment<T, P extends BasePresenter> extends BaseAppFragment<P> implements
        BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener,
        BaseQuickAdapter.OnItemLongClickListener {

    protected SwipeRefreshLayout mSwipeRefreshLayout;
    protected RecyclerView mRecyclerView;

    protected BaseQuickAdapter<T, BaseViewHolder> mAdapter;
    protected int mPageIndex = 1;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_base_page;
    }

    @Override
    protected void initView() {
        mSwipeRefreshLayout = mContainer.findViewById(R.id.swipe_layout_page);
        mRecyclerView = mContainer.findViewById(R.id.rcv_page);

        mSwipeRefreshLayout.setColorSchemeColors(ContextCompat.getColor(mContext, R.color.colorPrimary));

        mRecyclerView.setLayoutManager(getPageLayoutManager());
        mRecyclerView.setAdapter(mAdapter = getPageAdapter());
    }

    public RecyclerView.LayoutManager getPageLayoutManager() {
        return new LinearLayoutManager(mContext);
    }

    @Override
    protected void initListeners() {
        mSwipeRefreshLayout.setOnRefreshListener(() -> {
            mPageIndex = 1;
            initData();
        });

        mAdapter.setOnLoadMoreListener(this::initData, mRecyclerView);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemChildClickListener(this);
        mAdapter.setOnItemLongClickListener(this);
    }

    @Override
    public void showLoading(String message) {
        if (mSwipeRefreshLayout.isRefreshing() || mAdapter.isLoading()) {
            //do nothing
        } else {
            super.showLoading(message);
        }
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        if (mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void showEmptyView() {
        showEmptyView("暂无数据", R.mipmap.ic_empty);
    }

    /**
     * 第一页数据为空时候调用
     */
    protected void showEmptyView(String hint, int imgResId) {
        View emptyView = View.inflate(mContext, R.layout.layout_empty, null);
        TextView tvHint = emptyView.findViewById(R.id.tv_hint);
        ImageView ivHint = emptyView.findViewById(R.id.img_hint);

        tvHint.setText(hint);
        ivHint.setImageResource(imgResId);

        mAdapter.setEmptyView(emptyView);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
        return false;
    }

    /**
     * Adapter
     *
     * @return BaseQuickAdapter
     */
    public abstract BaseQuickAdapter getPageAdapter();

    /**
     * 获取实体分页数据
     */
    public abstract List<T> getPageEntities(Object obj);

    /**
     * 分页返回监听
     */
    protected class PageHttpResponseListener extends HttpResponseListener {

        public PageHttpResponseListener(IBaseView baseView) {
            super(baseView);
        }

        @Override
        public void onStart() {
            mSwipeRefreshLayout.setRefreshing(mPageIndex == 1);
            super.onStart();
        }

        @Override
        public void onSuccess(Object obj) {
            super.onSuccess(obj);
            List<T> pageEntities = getPageEntities(obj);
            if (pageEntities == null) {
                pageEntities = Collections.emptyList();
            }

            //处理数据
            if (mPageIndex == 1) {
                mAdapter.setNewData(pageEntities);
            } else {
                mAdapter.addData(pageEntities);
            }

            //处理界面
            if (mPageIndex == 1 && pageEntities.isEmpty()) {
                mAdapter.loadMoreEnd();
                showEmptyView();
            } else {
                if (pageEntities.isEmpty()) {
                    mAdapter.loadMoreEnd();
                } else {
                    mAdapter.loadMoreComplete();
                    mPageIndex++;
                }
            }
        }
    }
}
